package org.jeecg.modules.htxincailiao.cart.domain.vo;

import com.google.common.collect.Lists;
import lombok.Data;

import java.util.List;

/**
 * @author Administrator 2021-03-26
 * @version V1.0.0
 * @classname CartVO
 * @description description
 */
@Data
public class HTCartSessionVO {

    /**
     * 购物车集合, 每个店铺商品列表
     */
    private List<HTCartVO> goodsList = Lists.newArrayList();

    /**
     * 商品总数量
     */
    private int subtotalQuantity;

    /**
     * 商品总价格
     */
    private double subtotalAmount;

    /**
     * 商品总运费
     */
    private double subtotalFreight;

    /**
     * 商品店铺id
     */
    private String shopId;

    /**
     * 店铺名称
     */
    private String shopName;

    /**
     * 多个购物车id
     */
    private String cartIds;

    /**
     * 订单留言
     */
    private String orderMessage;

    /**
     * 选中状态
     */
    private boolean checked;

    /**
     * 部分选中
     */
    private boolean checkedIndeterminate;


    public void add(HTCartVO cart) {
        goodsList.add(cart);
    }

    public int update(HTCartVO cart) {
        //修改商品标识,若为0,商品修改时同一商品数量超过100
        int result = 1;
        for (HTCartVO goodsItem: goodsList) {
            if (goodsItem.getGoodsId().equals(cart.getGoodsId())) {
                //购物车同一件商品数量不能超过100
                if (goodsItem.getQuantity() + cart.getQuantity() <= 100) {
                    goodsItem.setQuantity( (goodsItem.getQuantity() + cart.getQuantity()));
                    result = -2;
                } else { //商品修改时同一商品数量超过100
                    result = 0;
                }
            }
        }
        //判断是否添加
        if (result == 1) {
            add(cart);
        }
        return result;
    }

    public void delete(String goodsId) {
        goodsList.removeIf(cart -> cart.getGoodsId( ) == goodsId);
    }

}
